package user

import (
	"context"
	"strconv"

	"mall/common/utils/pointy"
	"mall/service/backend/api/internal/svc"
	"mall/service/backend/api/internal/types"
	"mall/service/backend/rpc/backendclient"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateUserProfileLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUpdateUserProfileLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateUserProfileLogic {
	return &UpdateUserProfileLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UpdateUserProfileLogic) UpdateUserProfile(req *types.UpdateProfileReq) (resp *types.BaseMsgResp, err error) {
	var userId string = l.ctx.Value("userId").(string)
	var msg string
	if i, err := strconv.ParseInt(userId, 10, 64); err == nil && i > 0 {
		res, err := l.svcCtx.BackendRpc.UpdateUser(l.ctx, &backendclient.UserInfo{
			Id:       pointy.GetPointer(uint64(i)),
			Username: req.Username,
			Nickname: req.Nickname,
			Avatar:   req.Avatar,
			Mobile:   req.Mobile,
			Email:    req.Email,
		})
		if err != nil {
			return nil, err
		}

		msg = l.svcCtx.Trans.Trans(l.ctx, res.Msg)
	}

	return &types.BaseMsgResp{
		Success: true,
		Msg:     msg,
	}, nil
}
